package com.salesys.gr.gui;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;
import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.tony.domain.Withdraw;
import com.salesys.tony.service.UserService;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class FrmHandleWithdraw {

	protected Shell FrmHandleWithdraw;
	private Table tableHandleWithdraw;
	private String username = null;
	private String money = null;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmHandleWithdraw.open();
		FrmHandleWithdraw.layout();
		while (!FrmHandleWithdraw.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmHandleWithdraw = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmHandleWithdraw.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 27) {
					FrmHandleWithdraw.close();
				}
			}
		});
		FrmHandleWithdraw.setSize(300, 200);
		FrmHandleWithdraw.setText("处理提现申请");
		// 窗口居中
		FrmHandleWithdraw.setLocation(new FrmHelper().setLocationCenter(FrmHandleWithdraw));

		tableHandleWithdraw = new Table(FrmHandleWithdraw, SWT.BORDER | SWT.FULL_SELECTION);
		tableHandleWithdraw.setBounds(0, 0, 294, 90);
		tableHandleWithdraw.setHeaderVisible(true);
		tableHandleWithdraw.setLinesVisible(true);

		TableColumn tblclmnUsername = new TableColumn(tableHandleWithdraw, SWT.NONE);
		tblclmnUsername.setWidth(100);
		tblclmnUsername.setText("用户名");

		TableColumn tblclmnMoney = new TableColumn(tableHandleWithdraw, SWT.NONE);
		tblclmnMoney.setWidth(170);
		tblclmnMoney.setText("提现金额");

		List<Withdraw> withdrawList = new ArrayList<Withdraw>();
		try {
			withdrawList = new UserService().queryWithdraw();
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<TableItem> tbtmChild = new ArrayList<TableItem>();

		for (int i = 0; i < withdrawList.size(); i++) {
			tbtmChild.add(i, new TableItem(tableHandleWithdraw, SWT.NONE));
			tbtmChild.get(i).setText(0, withdrawList.get(i).getUsername());
			tbtmChild.get(i).setText(1, withdrawList.get(i).getMoney());
		}

		Label lblTip = new Label(FrmHandleWithdraw, SWT.NONE);
		lblTip.setBounds(4, 96, 288, 34);
		lblTip.setText("线下完成现金兑换后，\r\n选择用户点击已处理从列表中删除请求！");

		Button btnHandle = new Button(FrmHandleWithdraw, SWT.NONE);
		btnHandle.setBounds(204, 135, 80, 27);
		btnHandle.setText("已处理");

		tableHandleWithdraw.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				setUsername(((TableItem) e.item).getText(0));
				setMoney(((TableItem) e.item).getText(1));
			}
		});

		btnHandle.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 13) {
					if (username == null) {
						new MsgBox().errorTip(FrmHandleWithdraw, "请先选择一个用户！");
					} else {
						int affect1 = 0;
						int affect2 = 0;
						try {
							affect1 = new UserService().withdraw(username, money);
						} catch (Exception e) {
							e.printStackTrace();
						}
						if (affect1 == 1) {
							try {
								affect2 = new UserService().deleteWithdraw(username);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						if (affect1 == 1 && affect2 == 1) {
							new MsgBox().infoTip(FrmHandleWithdraw, username + "的提现请求已处理成功！");
							FrmHandleWithdraw.close();
						} else {
							new MsgBox().errorTip(FrmHandleWithdraw, "处理失败！");
						}
					}
				}
			}
		});
		btnHandle.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent arg1) {
				if (username == null) {
					new MsgBox().errorTip(FrmHandleWithdraw, "请先选择一个用户！");
				} else {
					int affect1 = 0;
					int affect2 = 0;
					try {
						affect1 = new UserService().withdraw(username, money);
					} catch (Exception e) {
						e.printStackTrace();
					}
					if (affect1 == 1) {
						try {
							affect2 = new UserService().deleteWithdraw(username);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					if (affect1 == 1 && affect2 == 1) {
						new MsgBox().infoTip(FrmHandleWithdraw, username + "的提现请求已处理成功！");
						FrmHandleWithdraw.close();
					} else {
						new MsgBox().errorTip(FrmHandleWithdraw, "处理失败！");
					}
				}
			}
		});
	}
}
